import java.util.HashSet;
import java.util.Set;

/**
 * 202. 快乐数
 * https://leetcode.cn/problems/happy-number/description/
 */
class Solution {
    public boolean isHappy(int n) {
        int cur = n;
        Set<Integer> set = new HashSet<>();
        while(cur > 1) {
            int x = 0;
            int tmp = cur;
            while(tmp > 0) {
                int t = tmp % 10;
                x += t * t;
                tmp /= 10;
            }
            if(set.contains(x)) return false;
            else set.add(x);
            cur = x;
        }
        return true;
    }
}